package com.example.wendao.view.ui.fragment;


import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.model.entity.Goods;
import com.example.wendao.model.entity.User;
import com.example.wendao.viewmodel.GoodsViewModel;
import com.example.wendao.viewmodel.UserViewModel;


public class MallGoodsInfoFragment extends Fragment {

    private TextView goodname, goodprice, goodinfo;
    private GoodsViewModel goodsViewModel = null;
    private UserViewModel userViewModel = null;
    private Button buy;
    private User user;
    private Goods good;

    public MallGoodsInfoFragment() {}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mall_goods_info, container, false);
        goodname = view.findViewById(R.id.mallinfo_good_name);
        goodprice = view.findViewById(R.id.mallinfo_good_price);
        goodinfo = view.findViewById(R.id.mallinfo_good_info);
        buy = view.findViewById(R.id.mallinfo_buy);
        goodsViewModel = new ViewModelProvider(App.getInstance()).get(GoodsViewModel.class);
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);
        user = userViewModel.getCurrentUser();
        Bundle bundle = getArguments();

        int goodid;
        if(bundle!=null){
            goodid=bundle.getInt("goodid");
            good = goodsViewModel.getGoodById(goodid);
        }
        goodname.setText("苹果");
        String s="价格：33个硬币";
        goodprice.setText(s);
        goodinfo.setText("权利声明：\n" +
                "京东上的所有商品信息、客户评价、商品咨询、网友讨论等内容，是京东重要的经营资源，未经许可，禁止非法转载使用。\n" +
                "注：本站商品信息均来自于合作方，其真实性、准确性和合法性由信息拥有者（合作方）负责。本站不提供任何保证，并不承担任何法律责任。\n" +
                "\n" +
                "\n" +
                "价格说明：\n" +
                "京东价：京东价为商品的销售价，是您最终决定是否购买商品的依据。\n" +
                "\n" +
                "划线价：商品展示的划横线价格为参考价，并非原价，该价格可能是品牌专柜标价、商品吊牌价或由品牌供应商提供的正品零售价（如厂商指导价、建议零售价等）或该商品在京东平台上曾经展示过的销售价；由于地区、时间的差异性和市场行情波动，品牌专柜标价、商品吊牌价等可能会与您购物时展示的不一致，该价格仅供您参考。\n" +
                "\n" +
                "折扣：如无特殊说明，折扣指销售商在原价、或划线价（如品牌专柜标价、商品吊牌价、厂商指导价、厂商建议零售价）等某一价格基础上计算出的优惠比例或优惠金额；如有疑问，您可在购买前联系销售商进行咨询。\n" +
                "\n" +
                "异常问题：商品促销信息以商品详情页“促销”栏中的信息为准；商品的具体售价以订单结算页价格为准；如您发现活动商品售价或促销信息有异常，建议购买前先联系销售商咨询");
        buy.setOnClickListener(view1 -> showCoverDialog());
        return view;
    }
    private void showCoverDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle("提示");
        builder.setMessage("您确定要购买吗");
        builder.setPositiveButton("确定", (dialogInterface, i) -> {
//            user.setCoins(user.getCoins() - good.getGoodPrice());
//            user.setUserGoods(user.getUserGoods()+","+good.getGoodId());
            Toast.makeText(getActivity(),"购买成功",Toast.LENGTH_SHORT).show();
        });

        builder.setNegativeButton("取消", (dialogInterface, i) -> {});
        builder.show();

    }
}
